<?php
namespace app\before\controller;

class ServiceBuy extends Base
{
    private $tn = 'service_buy';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            $where['b.status'] = ['neq',0];
            //根据id搜索
            if(isset($this->get['id'])){
                $where['b.id'] = $this->get['id'];
            }
            //根据服务
            if(isset($this->get['foreign_key'])){
                $where['foreign_key'] = $this->get['foreign_key'];
            }
            //根据购买时间
            if(isset($this->get['date'])){
                $where['b.pay_time'] = [['egt',strtotime($this->get['date'])],['lt',strtotime($this->get['date'].'+1 day')]];
            }
            //指定用户购买服务列表
            if(isset($this->get['buy_user'])){
                $where['buy_user'] = $this->get['buy_user'];
            }
            $list  = db($this->tn)->alias('b')->join(['bz_service' => 's'],'s.id = b.foreign_key')->field('s.title,b.*,FROM_UNIXTIME(pay_time,"%Y-%m-%d %H:%i") as pay_time')->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach($list as $key => $val){
                $list[$key]['finish_time'] = $val['status'] > 1? date('Y-m-d H:i',$val['finish_time']) : '';
            }
            $count = db($this->tn)->alias('b')->join(['bz_service' => 's'],'s.id = b.foreign_key')->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    
}
